﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using SoEasyPlatform.LayUiExample.Enties.DbModels;
using SqlSugar;

namespace SoEasyPlatform.LayUiExample.BusinessCore.DbCore
{
    public class DbContext<T> where T : class, new()
    {
        public DbContext()
        {
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Host=10.66.119.83;DataBase=layui_erp; Protocol=TCP; Port=3306; Compress=false; Charset=utf8; Pooling=true; Min Pool Size=0; Max Pool Size=200; Connection Lifetime=0; User id=root; Password=zizaiTrip123;",
                DbType = DbType.MySql,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

            });
            //调式代码 用来打印SQL 
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" +
                                  Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };

        }
        //注意：不能写成静态的
        public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
        public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }//用来操作当前表的数据

        public SimpleClient<bus_customer> bus_customerDb { get { return new SimpleClient<bus_customer>(Db); } }//用来处理bus_customer表的常用操作
        public SimpleClient<bus_goods> bus_goodsDb { get { return new SimpleClient<bus_goods>(Db); } }//用来处理bus_goods表的常用操作
        public SimpleClient<bus_inport> bus_inportDb { get { return new SimpleClient<bus_inport>(Db); } }//用来处理bus_inport表的常用操作
        public SimpleClient<bus_outport> bus_outportDb { get { return new SimpleClient<bus_outport>(Db); } }//用来处理bus_outport表的常用操作
        public SimpleClient<bus_provider> bus_providerDb { get { return new SimpleClient<bus_provider>(Db); } }//用来处理bus_provider表的常用操作
        public SimpleClient<bus_sales> bus_salesDb { get { return new SimpleClient<bus_sales>(Db); } }//用来处理bus_sales表的常用操作
        public SimpleClient<bus_salesback> bus_salesbackDb { get { return new SimpleClient<bus_salesback>(Db); } }//用来处理bus_salesback表的常用操作
        public SimpleClient<sys_dept> sys_deptDb { get { return new SimpleClient<sys_dept>(Db); } }//用来处理sys_dept表的常用操作
        public SimpleClient<sys_loginfo> sys_loginfoDb { get { return new SimpleClient<sys_loginfo>(Db); } }//用来处理sys_loginfo表的常用操作
        public SimpleClient<sys_notice> sys_noticeDb { get { return new SimpleClient<sys_notice>(Db); } }//用来处理sys_notice表的常用操作
        public SimpleClient<sys_permission> sys_permissionDb { get { return new SimpleClient<sys_permission>(Db); } }//用来处理sys_permission表的常用操作
        public SimpleClient<sys_role> sys_roleDb { get { return new SimpleClient<sys_role>(Db); } }//用来处理sys_role表的常用操作
        public SimpleClient<sys_role_permission> sys_role_permissionDb { get { return new SimpleClient<sys_role_permission>(Db); } }//用来处理sys_role_permission表的常用操作
        public SimpleClient<sys_role_user> sys_role_userDb { get { return new SimpleClient<sys_role_user>(Db); } }//用来处理sys_role_user表的常用操作
        public SimpleClient<sys_user> sys_userDb { get { return new SimpleClient<sys_user>(Db); } }//用来处理sys_user表的常用操作


        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public virtual List<T> GetList()
        {
            return CurrentDb.GetList();
        }

        /// <summary>
        /// 根据表达式查询
        /// </summary>
        /// <returns></returns>
        public virtual List<T> GetList(Expression<Func<T, bool>> whereExpression)
        {
            return CurrentDb.GetList(whereExpression);
        }


        /// <summary>
        /// 根据表达式查询分页
        /// </summary>
        /// <returns></returns>
        public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel pageModel)
        {
            return CurrentDb.GetPageList(whereExpression, pageModel);
        }

        /// <summary>
        /// 根据表达式查询分页并排序
        /// </summary>
        /// <param name="whereExpression">it</param>
        /// <param name="pageModel"></param>
        /// <param name="orderByExpression">it=>it.id或者it=>new{it.id,it.name}</param>
        /// <param name="orderByType">OrderByType.Desc</param>
        /// <returns></returns>
        public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel pageModel, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
        {
            return CurrentDb.GetPageList(whereExpression, pageModel,orderByExpression,orderByType);
        }


        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <returns></returns>
        public virtual T GetById(dynamic id)
        {
            return CurrentDb.GetById(id);
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Delete(dynamic id)
        {
            return CurrentDb.Delete(id);
        }


        /// <summary>
        /// 根据实体删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Delete(T data)
        {
            return CurrentDb.Delete(data);
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Delete(dynamic[] ids)
        {
            return CurrentDb.AsDeleteable().In(ids).ExecuteCommand()>0;
        }

        /// <summary>
        /// 根据表达式删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Delete(Expression<Func<T, bool>> whereExpression)
        {
            return CurrentDb.Delete(whereExpression);
        }


        /// <summary>
        /// 根据实体更新，实体需要有主键
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Update(T obj)
        {
            return CurrentDb.Update(obj);
        }

        /// <summary>
        ///批量更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Update(List<T> objs)
        {
            return CurrentDb.UpdateRange(objs);
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Insert(T obj)
        {
            return CurrentDb.Insert(obj);
        }


        /// <summary>
        /// 批量
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Insert(List<T> objs)
        {
            return CurrentDb.InsertRange(objs);
        }


        //自已扩展更多方法 
    }
}


